<?php
namespace Admin\Controller;
use Think\Controller;

/**
 *  财务流水控制器 
 */
 class BonusChangeController extends AdminController{
 	/**
 	 * 充值记录
 	 */
 	public function index(){
 		//搜索
 		$keyword = I('keyword', '', 'string');
 		$condition = array('like','%'.$keyword.'%');
		$map_user['username|email|mobile'] = array($condition,$condition, $condition,'_multi'=>true);
 		$userids=M('User')->where($map_user)->getField('id',true);

 		if(!empty($userids)){
 			$map['userid']=array('in',$userids);
 		}else{
 			$map['userid']=0;
 		}
 	
 		$keyselect=I('keyselect');
 		if(!empty($keyselect))
 		{
 			$map['type']=$keyselect;
 		}
 		//设置Tab导航数据列表
 		//获取可申请充值币种
 		$can_money_type_list=M('MoneyType')->where(array('status'=>1))->select();
 		if(empty($can_money_type_list)){
 			$this->error('请先添加币种!');
 		}
 		//默认币种
 		$money_type_default=$can_money_type_list[0]['identification'];
 		$can_money_type_arr=list_to_arr($can_money_type_list, 'identification', 'name');
 		foreach($can_money_type_arr as $key => $val){
 			$tab_list[$key]['title'] = $val;
 			$tab_list[$key]['href']  = U('index', array('group' => $key));
 		}
 		$group=I('group',$money_type_default);
 		$moneychange_model=D('Detailed'.ucfirst($group));
 		
 		$map['status']=1;
 		 
 		$starttime=I('starttime');
 		$endtime=I('endtime');
 		if(!empty($starttime) ) {
 			$map['create_time'][]=array('egt',strtotime($starttime));
 		}
 		if(!empty($endtime) ) {
 			$map['create_time'][]=array('lt',24*60*60+strtotime($endtime));
 		}
 		//预定义按钮属性以简化使用
 		$attr_bz['title'] = '查看备注';
 		$attr_bz['class'] = 'label label-warning';
 		$attr_bz['href'] ='javascript:void(0);';
 		$attr_bz['onclick']='show_bonus_change_info(__data_id__,"'.$group.'")';
 		 
 		//iframe层
 		$extra_html='<script type="text/javascript" src="./Public/extra/js/bonus_change_info.js" charset="utf-8"></script>';
 		 
 		$data_list = $moneychange_model->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
 		foreach ($data_list as $k=>$v)
 		{
 			if($v['money']>0)
 			{
 				$data_list[$k]['money']="<span style='color:green'>".$v['money']."</span>";
 			}else{
 				$data_list[$k]['money']="<span style='color:red'>".$v['money']."</span>";
 			}
 		}
 		$moneyType_list=M("descriptions")->where("tablename='detailed' and fieldname='type' and status=1")->field("fieldvalue,title")->select();
 		$attr['title'] = '导出数据';
 		$attr['class'] = 'btn btn-primary';
 		$attr['href']  = U('importData');
 		$page = new \Common\Util\Page($moneychange_model->where($map)->count(), C('ADMIN_PAGE_ROWS'));
 		//使用Builder快速建立列表页面。
 		$builder = new \Common\Builder\ListBuilder();
 		$builder->setMetaTitle('财务流水列表') //设置页面标题
 		->setSearch('请输入用户用户名/邮箱/手机号', U('index', array('group' => $group)),$moneyType_list,'','','1')
 		->SetTabNav($tab_list, $group); //设置Tab按钮列表
 		if($group=='bonus')
 		{
 		//$builder->addTopButton('self',$attr);
 		}
 		$builder->addTableColumn('id', 'ID')
 		->addTableColumn('userid', '用户','function','get_username')
 		->addTableColumn('relevant_userid', '相关用户','function','get_username')
 		->addTableColumn('type','流水类型','function','get_bonus_change_type_text')
 		->addTableColumn('money', '变更数量')
 		->addTableColumn('balance', '余额')
 		->addTableColumn('create_time', '流水时间', 'time')
 		->addTableColumn('right_button', '操作', 'btn')
 		->setTableDataList($data_list) //数据列表
 		->setTableDataPage($page->show()) //数据列表分页
 		->addRightButton('self',$attr_bz)//备注
 		->setExtraHtml($extra_html)
 		->display();
 	}
 	
 		
 public function importData()
 	{
 		$object=D('DetailedBonus');
 		$res2=$object->order('id desc')->select();	
 		if(empty($res2)){
 			$this->error('暂无信息！');
 		} 	
 		$str="序号,用户,类型,变更数量,余额,时间\n";
 		$str =iconv('utf-8','gb2312',$str);
 		//循环记录构建逗号分隔的字段信息 	
 		foreach($res2  as $row){
 			$id=iconv('utf-8','gb2312',$row['id']);
 			$user=get_user_data($row['userid'], 'user_name');
 			$user=iconv('utf-8','gb2312',$user);
 			$type=get_bonus_change_type_text($row['type']);
 			$type=iconv('utf-8','gb2312',$type);
 			$money=iconv('utf-8','gb2312',$row['money']);
 			$balance=iconv('utf-8','gb2312',$row['balance']);
 			$time=date('Y-m-d H:i:s',$row['create_time']);
 			$time=iconv('utf-8','gb2312',$time);
 			$str.=$id.",".$user.",".$type.",".$money.",".$balance.",".$time."\n"; //用引文逗号分开
 	
 		}
 		$filename = date('YmdHis').'Bonus.csv'; //设置文件名
 		$this->exportCsv($filename,$str);
 	
 	}
 	public function exportCsv($filename, $data){
 		header("Content-type:text/csv");
 		header("Content-Disposition:attachment;filename=".$filename);
 		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
 		header('Expires:0');
 		header('Pragma:public');
 		echo $data;
 	}
 	
 }